package nio.three;

import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.Scanner;

/*
    客户端发送消息的线程
 */
public class ClientSendThread extends Thread{
    private SocketChannel socketChannel;

    public ClientSendThread(SocketChannel socketChannel){
        this.socketChannel = socketChannel;
    }

    @Override
    public void run() {
        try {
            Scanner scanner = new Scanner(System.in);
            //创建缓冲区
            ByteBuffer buffer = ByteBuffer.allocate(1024);
            String str = "";
            while (true){
                //向缓冲区写入数据
                str = scanner.nextLine();
                buffer.put(str.getBytes());
                //将缓冲区中的数据发送给服务端
                buffer.flip();
                socketChannel.write(buffer);
                //清空缓冲区
                buffer.clear();
            }
        }catch (Exception e){

        }
    }
}
